{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.metrics import *\n",
    "from sklearn.linear_model import LogisticRegressionCV\n",
    "from sklearn.neural_network import MLPClassifier\n",
    "from sklearn.cross_validation import train_test_split,StratifiedKFold,KFold\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from model_disply import *\n",
    "from sklearn.externals import joblib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def model_lr(train_X,train_Y):\n",
    "    lg = LogisticRegressionCV(class_weight='balanced')\n",
    "    lg.fit(train_X,train_Y)  \n",
    "    return lg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def model_NN(train_X,train_Y):\n",
    "    nn = MLPClassifier(activation='logistic')\n",
    "    nn.fit(train_X,train_Y)\n",
    "    return nn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def model_CV(model_name):\n",
    "    ks_list = []\n",
    "#     weight = []\n",
    "#     bias =[]\n",
    "    for train,test in skf:\n",
    "        train_data = samples.iloc[train]\n",
    "        test_data = samples.iloc[test]\n",
    "        model = model_name(train_data.iloc[:,:-1],train_data.iloc[:,-1])\n",
    "        y_pro = model.predict_proba(test_data.iloc[:,:-1])\n",
    "        y_pro = [x[1] for x in y_pro]\n",
    "        ks = ks_value(test_data.iloc[:,-1],y_pro)        \n",
    "        ks_list.append(ks)\n",
    "#         weight.append(list(model.coef_[0]))\n",
    "#         bias.append(list(model.intercept_))\n",
    "    print ks_list\n",
    "    return np.mean([x[0] for x in ks_list])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#save model\n",
    "def save_model(model_name,model_s):\n",
    "    count=0\n",
    "    for train,test in skf:    \n",
    "        train_data = samples.iloc[train]\n",
    "        test_data = samples.iloc[test]\n",
    "        model = model_name(train_data.iloc[:,:-1],train_data.iloc[:,-1])\n",
    "        joblib.dump(model, \"./out1/%s_%d.m\"%(model_s,count))\n",
    "        test_data.to_csv('./data/model_test_data_%d.csv'%count)\n",
    "        count +=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#save validation data\n",
    "def save_valid_data():\n",
    "    for i in xrange(5):\n",
    "        train_X,val_X,train_Y,val_Y =train_test_split(samples.iloc[:,:-1],samples.iloc[:,-1],test_size=0.4)\n",
    "        val_X['label']=val_Y\n",
    "        val_X.to_csv('./out1/val_data_%d.csv'%i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def valid_model(model_name,model_num):\n",
    "    PSI =pd.DataFrame()\n",
    "    model1= joblib.load('./out1/%s_%d.m'%(model_name,model_num))\n",
    "    test_data = pd.read_csv('./data/model_test_data_%d.csv'%model_num,index_col=0)\n",
    "    y_pro = model1.predict_proba(test_data.iloc[:,:-1])\n",
    "    y_pro = [x[1] for x in y_pro]\n",
    "    t = pd.cut(y_pro,[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]).value_counts()\n",
    "    PSI['t'] = t.apply(lambda x:x*1.0/t.sum() if x>0 else 0.00001)\n",
    "    for i in xrange(5):\n",
    "        val_data =pd.read_csv('./out1/val_data_%d.csv'%i,index_col=0)\n",
    "        y_pre = model1.predict_proba(val_data.iloc[:,:-1])\n",
    "        y_pre = [x[1] for x in y_pre]\n",
    "        y_pre_bin = pd.cut(y_pre,[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]).value_counts()\n",
    "        PSI['t%d'%i] = y_pre_bin.apply(lambda x:x*1.0/y_pre_bin.sum() if x>0 else 0.00001)\n",
    "        ks =ks_value(val_data.iloc[:,-1],y_pre)\n",
    "        print ks\n",
    "    return PSI"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#cacilate psi\n",
    "def compute_psi(PSI):\n",
    "    psi_df =pd.DataFrame()\n",
    "    for col in PSI.columns[1:]:\n",
    "        PSI[col+'-t'] = PSI[col]-PSI['t']\n",
    "        PSI[col+'ln_t'] = np.log(PSI[col]*1.0/PSI['t'])\n",
    "        psi_df[col+'_psi']=PSI[col+'-t'] *PSI[col+'ln_t']\n",
    "    return psi_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#load data\n",
    "def train_model():\n",
    "    global samples,skf\n",
    "    samples = pd.read_csv('./out1/samples_for_card_fillna_mean.csv',index_col=0) #card\n",
    "#     samples = pd.read_csv('./out1/samples_41_fillna_mean.csv',index_col=0)\n",
    "    samples = samples.drop(['交易额最大值','货币资金','历史_交易总量', '历史_交易额总量'],axis=1)\n",
    "    skf = StratifiedKFold(samples.iloc[:,-1],n_folds=5)    \n",
    "    save_valid_data()    \n",
    "    lr_ks = model_CV(model_lr)\n",
    "    print lr_ks\n",
    "    print samples.head()\n",
    "#     nn_ks = model_CV(model_NN)\n",
    "#     print nn_ks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(0.357829220353789, 0.72330203101113788), (0.50386901424245822, 0.77526073791633954), (0.28787708870696427, 0.62795783335202426), (0.49994392732981952, 0.75356061455646517), (0.44802063474262643, 0.7607939890097567)]\n",
      "0.419507977075\n",
      "                     实收资本      注册地址      场地归属      注册时间    下游客户情况      涉诉信息  \\\n",
      "应收单号                                                                          \n",
      "YS2016030800082  0.274318 -0.439962  0.393246  0.290983 -0.484329  0.070214   \n",
      "YS2016030400009 -0.324096 -0.439962  0.393246 -0.164433 -0.086596 -0.093581   \n",
      "YS2016030400024  0.274318  0.203259  0.393246  0.290983  1.757263  1.329492   \n",
      "YS2016030700026 -0.324096  0.203259 -0.327458 -0.249272 -0.086596 -0.093581   \n",
      "YS2016030700041  0.274318  0.203259  0.393246  0.290983  1.757263  1.329492   \n",
      "\n",
      "                     有无贷款      企业征信       净资产      流动比率  ...    历史_交易额均值  \\\n",
      "应收单号                                                     ...               \n",
      "YS2016030800082  0.079079 -1.742430  0.139249 -0.501472  ...   -0.037529   \n",
      "YS2016030400009 -0.478549 -0.027717 -0.546015 -0.501472  ...   -0.446218   \n",
      "YS2016030400024  0.079079 -0.226458  0.139249  0.632134  ...   -0.077422   \n",
      "YS2016030700026 -0.478549 -0.027717 -0.546015 -0.798740  ...   -0.446218   \n",
      "YS2016030700041  0.079079 -0.226458  0.139249  0.632134  ...   -0.077422   \n",
      "\n",
      "                 历史_账期订单交易额均值     交易额均值     交易稳定性  历史_最近连续交易月数  历史_账期合作时间（月）  \\\n",
      "应收单号                                                                           \n",
      "YS2016030800082     -0.035634 -0.029661 -0.027321    -0.012667     -0.006778   \n",
      "YS2016030400009     -0.422121 -0.393759 -0.419708    -0.320472     -0.201534   \n",
      "YS2016030400024     -0.095122 -0.077422 -0.419708    -0.320472     -0.201534   \n",
      "YS2016030700026     -0.422121 -0.393759  0.341359     0.270350     -0.201534   \n",
      "YS2016030700041     -0.095122 -0.077422 -0.419708    -0.320472     -0.201534   \n",
      "\n",
      "                 订单时间间隔均值变化  账期订单交易量均值变化  账期订单交易额均值变化  label  \n",
      "应收单号                                                          \n",
      "YS2016030800082    0.032428     0.003712    -0.015402      1  \n",
      "YS2016030400009    0.032428     0.003712    -0.015402      0  \n",
      "YS2016030400024    0.094295    -0.123942     0.162763      0  \n",
      "YS2016030700026    0.094295    -0.123942     0.162763      0  \n",
      "YS2016030700041    0.094295    -0.123942     0.162763      0  \n",
      "\n",
      "[5 rows x 42 columns]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Index([u'实收资本', u'注册地址', u'场地归属', u'注册时间', u'下游客户情况', u'涉诉信息', u'有无贷款',\n",
       "       u'企业征信', u'净资产', u'流动比率', u'资产负债率', u'主营业务利润率', u'应收账款周转天数', u'存货周转天数',\n",
       "       u'季度逾期占比', u'季度平均逾期天数', u'历史_逾期占比', u'历史_提前还款占比', u'季度提前还款占比',\n",
       "       u'历史_平均逾期天数', u'平均逾期天数变化', u'逾期占比变化', u'订单总数', u'交易额总量', u'历史_交易稳定性',\n",
       "       u'交易量最小值', u'交易总量', u'交易额最小值', u'交易额方差', u'交易量方差', u'提前还款占比变化',\n",
       "       u'历史_账期订单交易量均值', u'历史_交易额均值', u'历史_账期订单交易额均值', u'交易额均值', u'交易稳定性',\n",
       "       u'历史_最近连续交易月数', u'历史_账期合作时间（月）', u'订单时间间隔均值变化', u'账期订单交易量均值变化',\n",
       "       u'账期订单交易额均值变化', u'label'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_model()\n",
    "samples.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0.53868038078564395, 0.82169821643505858)\n",
      "(0.4649765439239123, 0.77843647580489694)\n",
      "(0.44707752506725362, 0.77297627781853762)\n",
      "(0.49333547454633048, 0.82218564316685416)\n",
      "(0.4685961264908633, 0.79319400372031934)\n",
      "(0.48044096728307256, 0.79155268628952835)\n",
      "(0.42352676563202879, 0.74415204678362579)\n",
      "(0.43695280019564681, 0.74942528735632175)\n",
      "(0.50438948664418393, 0.79598254911407307)\n",
      "(0.44925593609804138, 0.77330670751723374)\n",
      "(0.50946493051756203, 0.80944304628515151)\n",
      "(0.45623674571042988, 0.7587076666024033)\n",
      "(0.42712154561017363, 0.76128637808755206)\n",
      "(0.48201381082383171, 0.81008778973288376)\n",
      "(0.47483313272786953, 0.79002079002079006)\n",
      "(0.50815187657292915, 0.80003282634861583)\n",
      "(0.41629715313925836, 0.76073195810037919)\n",
      "(0.42377109317681577, 0.7678405478112007)\n",
      "(0.48319147797227124, 0.81083721428189071)\n",
      "(0.44714957872852612, 0.78922748659590758)\n",
      "(0.51359557938505307, 0.80678958310537252)\n",
      "(0.45324850588008486, 0.75983227299016776)\n",
      "(0.45267791636096849, 0.76769381266813408)\n",
      "(0.50824367003907711, 0.81394197312777694)\n",
      "(0.48659590764853916, 0.79341284604442508)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>lr_0</th>\n",
       "      <th>lr_1</th>\n",
       "      <th>lr_2</th>\n",
       "      <th>lr_3</th>\n",
       "      <th>lr_4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>t0_psi</th>\n",
       "      <td>0.082038</td>\n",
       "      <td>0.214402</td>\n",
       "      <td>0.223219</td>\n",
       "      <td>0.162954</td>\n",
       "      <td>0.337092</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t1_psi</th>\n",
       "      <td>0.127748</td>\n",
       "      <td>0.226547</td>\n",
       "      <td>0.205913</td>\n",
       "      <td>0.192872</td>\n",
       "      <td>0.291612</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t2_psi</th>\n",
       "      <td>0.146494</td>\n",
       "      <td>0.278985</td>\n",
       "      <td>0.242612</td>\n",
       "      <td>0.094266</td>\n",
       "      <td>0.171121</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t3_psi</th>\n",
       "      <td>0.058798</td>\n",
       "      <td>0.214029</td>\n",
       "      <td>0.212020</td>\n",
       "      <td>0.144775</td>\n",
       "      <td>0.309916</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>t4_psi</th>\n",
       "      <td>0.064377</td>\n",
       "      <td>0.194701</td>\n",
       "      <td>0.244799</td>\n",
       "      <td>0.237909</td>\n",
       "      <td>0.359856</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            lr_0      lr_1      lr_2      lr_3      lr_4\n",
       "t0_psi  0.082038  0.214402  0.223219  0.162954  0.337092\n",
       "t1_psi  0.127748  0.226547  0.205913  0.192872  0.291612\n",
       "t2_psi  0.146494  0.278985  0.242612  0.094266  0.171121\n",
       "t3_psi  0.058798  0.214029  0.212020  0.144775  0.309916\n",
       "t4_psi  0.064377  0.194701  0.244799  0.237909  0.359856"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "save_model(model_lr,'model_lr')\n",
    "P=pd.DataFrame()\n",
    "psi_sum =pd.DataFrame()\n",
    "for i in xrange(5):\n",
    "    PSI = valid_model('model_lr',i)\n",
    "    P = pd.concat([P,PSI])\n",
    "    psi = compute_psi(PSI)\n",
    "    psi_sum['lr_%d'%i] = psi.sum()\n",
    "    \n",
    "psi_sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>t</th>\n",
       "      <th>t0</th>\n",
       "      <th>t1</th>\n",
       "      <th>t2</th>\n",
       "      <th>t3</th>\n",
       "      <th>t4</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>(0.0, 0.1]</th>\n",
       "      <td>0.132616</td>\n",
       "      <td>0.127469</td>\n",
       "      <td>0.113106</td>\n",
       "      <td>0.132855</td>\n",
       "      <td>0.140036</td>\n",
       "      <td>0.152603</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.1, 0.2]</th>\n",
       "      <td>0.168459</td>\n",
       "      <td>0.166966</td>\n",
       "      <td>0.192101</td>\n",
       "      <td>0.199282</td>\n",
       "      <td>0.163375</td>\n",
       "      <td>0.159785</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.2, 0.3]</th>\n",
       "      <td>0.179211</td>\n",
       "      <td>0.152603</td>\n",
       "      <td>0.183124</td>\n",
       "      <td>0.154399</td>\n",
       "      <td>0.165171</td>\n",
       "      <td>0.156194</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.3, 0.4]</th>\n",
       "      <td>0.103943</td>\n",
       "      <td>0.105925</td>\n",
       "      <td>0.091562</td>\n",
       "      <td>0.077199</td>\n",
       "      <td>0.089767</td>\n",
       "      <td>0.078995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.4, 0.5]</th>\n",
       "      <td>0.111111</td>\n",
       "      <td>0.100539</td>\n",
       "      <td>0.080790</td>\n",
       "      <td>0.091562</td>\n",
       "      <td>0.111311</td>\n",
       "      <td>0.098743</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.5, 0.6]</th>\n",
       "      <td>0.093190</td>\n",
       "      <td>0.078995</td>\n",
       "      <td>0.098743</td>\n",
       "      <td>0.091562</td>\n",
       "      <td>0.096948</td>\n",
       "      <td>0.104129</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.6, 0.7]</th>\n",
       "      <td>0.082437</td>\n",
       "      <td>0.113106</td>\n",
       "      <td>0.105925</td>\n",
       "      <td>0.118492</td>\n",
       "      <td>0.105925</td>\n",
       "      <td>0.104129</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.7, 0.8]</th>\n",
       "      <td>0.035842</td>\n",
       "      <td>0.091562</td>\n",
       "      <td>0.095153</td>\n",
       "      <td>0.096948</td>\n",
       "      <td>0.073609</td>\n",
       "      <td>0.078995</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.8, 0.9]</th>\n",
       "      <td>0.050179</td>\n",
       "      <td>0.032316</td>\n",
       "      <td>0.021544</td>\n",
       "      <td>0.023339</td>\n",
       "      <td>0.030521</td>\n",
       "      <td>0.034111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.9, 1.0]</th>\n",
       "      <td>0.043011</td>\n",
       "      <td>0.030521</td>\n",
       "      <td>0.017953</td>\n",
       "      <td>0.014363</td>\n",
       "      <td>0.023339</td>\n",
       "      <td>0.032316</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.0, 0.1]</th>\n",
       "      <td>0.000010</td>\n",
       "      <td>0.000010</td>\n",
       "      <td>0.000010</td>\n",
       "      <td>0.000010</td>\n",
       "      <td>0.000010</td>\n",
       "      <td>0.000010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.1, 0.2]</th>\n",
       "      <td>0.025180</td>\n",
       "      <td>0.078995</td>\n",
       "      <td>0.080790</td>\n",
       "      <td>0.078995</td>\n",
       "      <td>0.082585</td>\n",
       "      <td>0.086176</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.2, 0.3]</th>\n",
       "      <td>0.172662</td>\n",
       "      <td>0.206463</td>\n",
       "      <td>0.210054</td>\n",
       "      <td>0.231598</td>\n",
       "      <td>0.217235</td>\n",
       "      <td>0.202873</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.3, 0.4]</th>\n",
       "      <td>0.129496</td>\n",
       "      <td>0.150808</td>\n",
       "      <td>0.136445</td>\n",
       "      <td>0.145422</td>\n",
       "      <td>0.140036</td>\n",
       "      <td>0.147217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.4, 0.5]</th>\n",
       "      <td>0.226619</td>\n",
       "      <td>0.154399</td>\n",
       "      <td>0.195691</td>\n",
       "      <td>0.143627</td>\n",
       "      <td>0.172352</td>\n",
       "      <td>0.165171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.5, 0.6]</th>\n",
       "      <td>0.169065</td>\n",
       "      <td>0.165171</td>\n",
       "      <td>0.170557</td>\n",
       "      <td>0.170557</td>\n",
       "      <td>0.168761</td>\n",
       "      <td>0.174147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.6, 0.7]</th>\n",
       "      <td>0.197842</td>\n",
       "      <td>0.154399</td>\n",
       "      <td>0.138241</td>\n",
       "      <td>0.166966</td>\n",
       "      <td>0.147217</td>\n",
       "      <td>0.149013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.7, 0.8]</th>\n",
       "      <td>0.010791</td>\n",
       "      <td>0.057451</td>\n",
       "      <td>0.050269</td>\n",
       "      <td>0.050269</td>\n",
       "      <td>0.048474</td>\n",
       "      <td>0.044883</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.8, 0.9]</th>\n",
       "      <td>0.068345</td>\n",
       "      <td>0.032316</td>\n",
       "      <td>0.017953</td>\n",
       "      <td>0.012567</td>\n",
       "      <td>0.023339</td>\n",
       "      <td>0.030521</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.9, 1.0]</th>\n",
       "      <td>0.000010</td>\n",
       "      <td>0.000010</td>\n",
       "      <td>0.000010</td>\n",
       "      <td>0.000010</td>\n",
       "      <td>0.000010</td>\n",
       "      <td>0.000010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.0, 0.1]</th>\n",
       "      <td>0.147482</td>\n",
       "      <td>0.150808</td>\n",
       "      <td>0.156194</td>\n",
       "      <td>0.195691</td>\n",
       "      <td>0.166966</td>\n",
       "      <td>0.192101</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.1, 0.2]</th>\n",
       "      <td>0.143885</td>\n",
       "      <td>0.172352</td>\n",
       "      <td>0.168761</td>\n",
       "      <td>0.154399</td>\n",
       "      <td>0.163375</td>\n",
       "      <td>0.149013</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.2, 0.3]</th>\n",
       "      <td>0.075540</td>\n",
       "      <td>0.098743</td>\n",
       "      <td>0.136445</td>\n",
       "      <td>0.098743</td>\n",
       "      <td>0.105925</td>\n",
       "      <td>0.089767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.3, 0.4]</th>\n",
       "      <td>0.161871</td>\n",
       "      <td>0.107720</td>\n",
       "      <td>0.091562</td>\n",
       "      <td>0.104129</td>\n",
       "      <td>0.116697</td>\n",
       "      <td>0.102334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.4, 0.5]</th>\n",
       "      <td>0.115108</td>\n",
       "      <td>0.120287</td>\n",
       "      <td>0.127469</td>\n",
       "      <td>0.111311</td>\n",
       "      <td>0.116697</td>\n",
       "      <td>0.129264</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.5, 0.6]</th>\n",
       "      <td>0.154676</td>\n",
       "      <td>0.102334</td>\n",
       "      <td>0.104129</td>\n",
       "      <td>0.105925</td>\n",
       "      <td>0.111311</td>\n",
       "      <td>0.109515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.6, 0.7]</th>\n",
       "      <td>0.118705</td>\n",
       "      <td>0.107720</td>\n",
       "      <td>0.107720</td>\n",
       "      <td>0.100539</td>\n",
       "      <td>0.091562</td>\n",
       "      <td>0.080790</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.7, 0.8]</th>\n",
       "      <td>0.053957</td>\n",
       "      <td>0.087971</td>\n",
       "      <td>0.077199</td>\n",
       "      <td>0.098743</td>\n",
       "      <td>0.080790</td>\n",
       "      <td>0.091562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.8, 0.9]</th>\n",
       "      <td>0.000010</td>\n",
       "      <td>0.019749</td>\n",
       "      <td>0.012567</td>\n",
       "      <td>0.017953</td>\n",
       "      <td>0.019749</td>\n",
       "      <td>0.019749</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.9, 1.0]</th>\n",
       "      <td>0.028777</td>\n",
       "      <td>0.032316</td>\n",
       "      <td>0.017953</td>\n",
       "      <td>0.012567</td>\n",
       "      <td>0.026930</td>\n",
       "      <td>0.035907</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.0, 0.1]</th>\n",
       "      <td>0.032374</td>\n",
       "      <td>0.008977</td>\n",
       "      <td>0.008977</td>\n",
       "      <td>0.014363</td>\n",
       "      <td>0.016158</td>\n",
       "      <td>0.017953</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.1, 0.2]</th>\n",
       "      <td>0.190647</td>\n",
       "      <td>0.179533</td>\n",
       "      <td>0.197487</td>\n",
       "      <td>0.201077</td>\n",
       "      <td>0.177738</td>\n",
       "      <td>0.195691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.2, 0.3]</th>\n",
       "      <td>0.179856</td>\n",
       "      <td>0.193896</td>\n",
       "      <td>0.193896</td>\n",
       "      <td>0.197487</td>\n",
       "      <td>0.179533</td>\n",
       "      <td>0.175943</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.3, 0.4]</th>\n",
       "      <td>0.104317</td>\n",
       "      <td>0.116697</td>\n",
       "      <td>0.129264</td>\n",
       "      <td>0.131059</td>\n",
       "      <td>0.138241</td>\n",
       "      <td>0.125673</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.4, 0.5]</th>\n",
       "      <td>0.086331</td>\n",
       "      <td>0.138241</td>\n",
       "      <td>0.140036</td>\n",
       "      <td>0.098743</td>\n",
       "      <td>0.154399</td>\n",
       "      <td>0.140036</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.5, 0.6]</th>\n",
       "      <td>0.122302</td>\n",
       "      <td>0.105925</td>\n",
       "      <td>0.113106</td>\n",
       "      <td>0.111311</td>\n",
       "      <td>0.120287</td>\n",
       "      <td>0.104129</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.6, 0.7]</th>\n",
       "      <td>0.215827</td>\n",
       "      <td>0.170557</td>\n",
       "      <td>0.152603</td>\n",
       "      <td>0.179533</td>\n",
       "      <td>0.138241</td>\n",
       "      <td>0.141831</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.7, 0.8]</th>\n",
       "      <td>0.028777</td>\n",
       "      <td>0.037702</td>\n",
       "      <td>0.034111</td>\n",
       "      <td>0.044883</td>\n",
       "      <td>0.034111</td>\n",
       "      <td>0.050269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.8, 0.9]</th>\n",
       "      <td>0.039568</td>\n",
       "      <td>0.035907</td>\n",
       "      <td>0.017953</td>\n",
       "      <td>0.016158</td>\n",
       "      <td>0.034111</td>\n",
       "      <td>0.028725</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.9, 1.0]</th>\n",
       "      <td>0.000010</td>\n",
       "      <td>0.012567</td>\n",
       "      <td>0.012567</td>\n",
       "      <td>0.005386</td>\n",
       "      <td>0.007181</td>\n",
       "      <td>0.019749</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.0, 0.1]</th>\n",
       "      <td>0.053957</td>\n",
       "      <td>0.017953</td>\n",
       "      <td>0.017953</td>\n",
       "      <td>0.026930</td>\n",
       "      <td>0.023339</td>\n",
       "      <td>0.025135</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.1, 0.2]</th>\n",
       "      <td>0.201439</td>\n",
       "      <td>0.222621</td>\n",
       "      <td>0.215440</td>\n",
       "      <td>0.228007</td>\n",
       "      <td>0.226212</td>\n",
       "      <td>0.229803</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.2, 0.3]</th>\n",
       "      <td>0.143885</td>\n",
       "      <td>0.127469</td>\n",
       "      <td>0.141831</td>\n",
       "      <td>0.134650</td>\n",
       "      <td>0.107720</td>\n",
       "      <td>0.122083</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.3, 0.4]</th>\n",
       "      <td>0.154676</td>\n",
       "      <td>0.156194</td>\n",
       "      <td>0.166966</td>\n",
       "      <td>0.159785</td>\n",
       "      <td>0.174147</td>\n",
       "      <td>0.157989</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.4, 0.5]</th>\n",
       "      <td>0.082734</td>\n",
       "      <td>0.120287</td>\n",
       "      <td>0.132855</td>\n",
       "      <td>0.102334</td>\n",
       "      <td>0.149013</td>\n",
       "      <td>0.132855</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.5, 0.6]</th>\n",
       "      <td>0.064748</td>\n",
       "      <td>0.107720</td>\n",
       "      <td>0.113106</td>\n",
       "      <td>0.114901</td>\n",
       "      <td>0.100539</td>\n",
       "      <td>0.100539</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.6, 0.7]</th>\n",
       "      <td>0.133094</td>\n",
       "      <td>0.129264</td>\n",
       "      <td>0.113106</td>\n",
       "      <td>0.143627</td>\n",
       "      <td>0.116697</td>\n",
       "      <td>0.116697</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.7, 0.8]</th>\n",
       "      <td>0.154676</td>\n",
       "      <td>0.075404</td>\n",
       "      <td>0.073609</td>\n",
       "      <td>0.070018</td>\n",
       "      <td>0.062837</td>\n",
       "      <td>0.068223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.8, 0.9]</th>\n",
       "      <td>0.010791</td>\n",
       "      <td>0.017953</td>\n",
       "      <td>0.007181</td>\n",
       "      <td>0.012567</td>\n",
       "      <td>0.023339</td>\n",
       "      <td>0.019749</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(0.9, 1.0]</th>\n",
       "      <td>0.000010</td>\n",
       "      <td>0.025135</td>\n",
       "      <td>0.017953</td>\n",
       "      <td>0.007181</td>\n",
       "      <td>0.016158</td>\n",
       "      <td>0.026930</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   t        t0        t1        t2        t3        t4\n",
       "(0.0, 0.1]  0.132616  0.127469  0.113106  0.132855  0.140036  0.152603\n",
       "(0.1, 0.2]  0.168459  0.166966  0.192101  0.199282  0.163375  0.159785\n",
       "(0.2, 0.3]  0.179211  0.152603  0.183124  0.154399  0.165171  0.156194\n",
       "(0.3, 0.4]  0.103943  0.105925  0.091562  0.077199  0.089767  0.078995\n",
       "(0.4, 0.5]  0.111111  0.100539  0.080790  0.091562  0.111311  0.098743\n",
       "(0.5, 0.6]  0.093190  0.078995  0.098743  0.091562  0.096948  0.104129\n",
       "(0.6, 0.7]  0.082437  0.113106  0.105925  0.118492  0.105925  0.104129\n",
       "(0.7, 0.8]  0.035842  0.091562  0.095153  0.096948  0.073609  0.078995\n",
       "(0.8, 0.9]  0.050179  0.032316  0.021544  0.023339  0.030521  0.034111\n",
       "(0.9, 1.0]  0.043011  0.030521  0.017953  0.014363  0.023339  0.032316\n",
       "(0.0, 0.1]  0.000010  0.000010  0.000010  0.000010  0.000010  0.000010\n",
       "(0.1, 0.2]  0.025180  0.078995  0.080790  0.078995  0.082585  0.086176\n",
       "(0.2, 0.3]  0.172662  0.206463  0.210054  0.231598  0.217235  0.202873\n",
       "(0.3, 0.4]  0.129496  0.150808  0.136445  0.145422  0.140036  0.147217\n",
       "(0.4, 0.5]  0.226619  0.154399  0.195691  0.143627  0.172352  0.165171\n",
       "(0.5, 0.6]  0.169065  0.165171  0.170557  0.170557  0.168761  0.174147\n",
       "(0.6, 0.7]  0.197842  0.154399  0.138241  0.166966  0.147217  0.149013\n",
       "(0.7, 0.8]  0.010791  0.057451  0.050269  0.050269  0.048474  0.044883\n",
       "(0.8, 0.9]  0.068345  0.032316  0.017953  0.012567  0.023339  0.030521\n",
       "(0.9, 1.0]  0.000010  0.000010  0.000010  0.000010  0.000010  0.000010\n",
       "(0.0, 0.1]  0.147482  0.150808  0.156194  0.195691  0.166966  0.192101\n",
       "(0.1, 0.2]  0.143885  0.172352  0.168761  0.154399  0.163375  0.149013\n",
       "(0.2, 0.3]  0.075540  0.098743  0.136445  0.098743  0.105925  0.089767\n",
       "(0.3, 0.4]  0.161871  0.107720  0.091562  0.104129  0.116697  0.102334\n",
       "(0.4, 0.5]  0.115108  0.120287  0.127469  0.111311  0.116697  0.129264\n",
       "(0.5, 0.6]  0.154676  0.102334  0.104129  0.105925  0.111311  0.109515\n",
       "(0.6, 0.7]  0.118705  0.107720  0.107720  0.100539  0.091562  0.080790\n",
       "(0.7, 0.8]  0.053957  0.087971  0.077199  0.098743  0.080790  0.091562\n",
       "(0.8, 0.9]  0.000010  0.019749  0.012567  0.017953  0.019749  0.019749\n",
       "(0.9, 1.0]  0.028777  0.032316  0.017953  0.012567  0.026930  0.035907\n",
       "(0.0, 0.1]  0.032374  0.008977  0.008977  0.014363  0.016158  0.017953\n",
       "(0.1, 0.2]  0.190647  0.179533  0.197487  0.201077  0.177738  0.195691\n",
       "(0.2, 0.3]  0.179856  0.193896  0.193896  0.197487  0.179533  0.175943\n",
       "(0.3, 0.4]  0.104317  0.116697  0.129264  0.131059  0.138241  0.125673\n",
       "(0.4, 0.5]  0.086331  0.138241  0.140036  0.098743  0.154399  0.140036\n",
       "(0.5, 0.6]  0.122302  0.105925  0.113106  0.111311  0.120287  0.104129\n",
       "(0.6, 0.7]  0.215827  0.170557  0.152603  0.179533  0.138241  0.141831\n",
       "(0.7, 0.8]  0.028777  0.037702  0.034111  0.044883  0.034111  0.050269\n",
       "(0.8, 0.9]  0.039568  0.035907  0.017953  0.016158  0.034111  0.028725\n",
       "(0.9, 1.0]  0.000010  0.012567  0.012567  0.005386  0.007181  0.019749\n",
       "(0.0, 0.1]  0.053957  0.017953  0.017953  0.026930  0.023339  0.025135\n",
       "(0.1, 0.2]  0.201439  0.222621  0.215440  0.228007  0.226212  0.229803\n",
       "(0.2, 0.3]  0.143885  0.127469  0.141831  0.134650  0.107720  0.122083\n",
       "(0.3, 0.4]  0.154676  0.156194  0.166966  0.159785  0.174147  0.157989\n",
       "(0.4, 0.5]  0.082734  0.120287  0.132855  0.102334  0.149013  0.132855\n",
       "(0.5, 0.6]  0.064748  0.107720  0.113106  0.114901  0.100539  0.100539\n",
       "(0.6, 0.7]  0.133094  0.129264  0.113106  0.143627  0.116697  0.116697\n",
       "(0.7, 0.8]  0.154676  0.075404  0.073609  0.070018  0.062837  0.068223\n",
       "(0.8, 0.9]  0.010791  0.017953  0.007181  0.012567  0.023339  0.019749\n",
       "(0.9, 1.0]  0.000010  0.025135  0.017953  0.007181  0.016158  0.026930"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "P\n",
    "# samples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 模型计分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def calc_score(p):\n",
    "    odds = np.log(p/(1-p))\n",
    "    A=441.5\n",
    "    B=72.13\n",
    "    score =A-B*odds\n",
    "    return int(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0.47569267835631113, 0.7973229821978316)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>lr_pre</th>\n",
       "      <th>score</th>\n",
       "      <th>rank_score</th>\n",
       "      <th>label</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>应收单号</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>YS2016030800082</th>\n",
       "      <td>0.194267</td>\n",
       "      <td>544</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>YS2016030400009</th>\n",
       "      <td>0.632550</td>\n",
       "      <td>402</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>YS2016030400024</th>\n",
       "      <td>0.593701</td>\n",
       "      <td>414</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>YS2016030700026</th>\n",
       "      <td>0.244687</td>\n",
       "      <td>522</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>YS2016030700041</th>\n",
       "      <td>0.298434</td>\n",
       "      <td>503</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   lr_pre  score  rank_score  label\n",
       "应收单号                                               \n",
       "YS2016030800082  0.194267    544           3      1\n",
       "YS2016030400009  0.632550    402           1      0\n",
       "YS2016030400024  0.593701    414           1      0\n",
       "YS2016030700026  0.244687    522           3      0\n",
       "YS2016030700041  0.298434    503           2      0"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#最优模型用于预测所有样本\n",
    "model1 = joblib.load('./out1/model_lr_0.m')\n",
    "pre = model1.predict_proba(samples.iloc[:,:-1])\n",
    "result =pd.DataFrame(index=samples.index)\n",
    "pre = [x[1] for x in pre]\n",
    "result['lr_pre'] = list(pre)\n",
    "result['score'] = result['lr_pre'].apply(lambda x:calc_score(x))\n",
    "result['rank_score'] =result['score'].rank(method='max') /(1391/5.0)\n",
    "result['rank_score'] =result['rank_score'].apply(lambda x:int(x) if x>0 and x<5 else 4)\n",
    "result['label'] =samples['label']\n",
    "print ks_value(samples.iloc[:,-1],pre)\n",
    "result.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/sf/.local/lib/python2.7/site-packages/matplotlib/legend.py:326: UserWarning: Unrecognized location \"mid right\". Falling back on \"best\"; valid locations are\n",
      "\tright\n",
      "\tcenter left\n",
      "\tupper right\n",
      "\tlower right\n",
      "\tbest\n",
      "\tcenter\n",
      "\tlower left\n",
      "\tcenter right\n",
      "\tupper left\n",
      "\tupper center\n",
      "\tlower center\n",
      "\n",
      "  six.iterkeys(self.codes))))\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-16-9b5f0989f421>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mks_plot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msamples\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0miloc\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mpre\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m'all_samples_ks_6'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;31m# auc_plot(samples.iloc[:,-1],pre,'all_samples_auc')\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m/home/sf/PycharmProjects/sparkdata/lt/mingshengjiayin/model_disply.pyc\u001b[0m in \u001b[0;36mks_plot\u001b[1;34m(y_true, y_pre, model_name)\u001b[0m\n\u001b[0;32m    107\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mmodel_name\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    108\u001b[0m         \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msavefig\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'./new_figure/%s_%.2f.png'\u001b[0m\u001b[1;33m%\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmodel_name\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mks_value\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 109\u001b[1;33m     \u001b[0mplt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    110\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    111\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mauc_plot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my_true\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0my_pre\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mmodel_name\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m/home/sf/.local/lib/python2.7/site-packages/matplotlib/pyplot.pyc\u001b[0m in \u001b[0;36mshow\u001b[1;34m(*args, **kw)\u001b[0m\n\u001b[0;32m    251\u001b[0m     \"\"\"\n\u001b[0;32m    252\u001b[0m     \u001b[1;32mglobal\u001b[0m \u001b[0m_show\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 253\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0m_show\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkw\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    254\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    255\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m/home/sf/.local/lib/python2.7/site-packages/matplotlib/backend_bases.pyc\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, block)\u001b[0m\n\u001b[0;32m    191\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    192\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mis_interactive\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mget_backend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'WebAgg'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 193\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmainloop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    194\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    195\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mmainloop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m/home/sf/.local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.pyc\u001b[0m in \u001b[0;36mmainloop\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     69\u001b[0m \u001b[1;32mclass\u001b[0m \u001b[0mShow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mShowBase\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     70\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mmainloop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 71\u001b[1;33m         \u001b[0mTk\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmainloop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     72\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     73\u001b[0m \u001b[0mshow\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mShow\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m/home/sf/anaconda2/lib/python2.7/lib-tk/Tkinter.pyc\u001b[0m in \u001b[0;36mmainloop\u001b[1;34m(n)\u001b[0m\n\u001b[0;32m    417\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmainloop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    418\u001b[0m     \u001b[1;34m\"\"\"Run the main loop of Tcl.\"\"\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 419\u001b[1;33m     \u001b[0m_default_root\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtk\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmainloop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mn\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    420\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    421\u001b[0m \u001b[0mgetint\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mint\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "ks_plot(samples.iloc[:,-1],pre,'all_samples_ks_6')\n",
    "# auc_plot(samples.iloc[:,-1],pre,'all_samples_auc')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#概率分布\n",
    "pd.cut(pre,[0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]).value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#等量统计\n",
    "tmp =pd.DataFrame()\n",
    "tmp['等级界点'] = result.groupby(by='rank_score')['score'].apply(lambda x:(x.min(),x.max()))\n",
    "tmp['样本数'] = result['rank_score'].value_counts()\n",
    "tmp['违约样本数'] = result.groupby(by='rank_score')['label'].sum()\n",
    "tmp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>样本数</th>\n",
       "      <th>违约样本数</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>(490, 590]</th>\n",
       "      <td>477</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(390, 490]</th>\n",
       "      <td>462</td>\n",
       "      <td>79</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(290, 390]</th>\n",
       "      <td>200</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(590, 690]</th>\n",
       "      <td>186</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(190, 290]</th>\n",
       "      <td>40</td>\n",
       "      <td>21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(690, 790]</th>\n",
       "      <td>26</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            样本数  违约样本数\n",
       "(490, 590]  477     17\n",
       "(390, 490]  462     79\n",
       "(290, 390]  200     66\n",
       "(590, 690]  186      3\n",
       "(190, 290]   40     21\n",
       "(690, 790]   26      0"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#等距统计\n",
    "tmp =pd.DataFrame()\n",
    "result['score_bin']= pd.cut(result['score'],[x for x in xrange(190,800,100)])\n",
    "tmp ['样本数'] =result['score_bin'].value_counts()\n",
    "tmp['违约样本数'] = result.groupby(by='score_bin')['label'].sum()\n",
    "tmp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 评分卡计分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "wei_sf = pd.DataFrame(index=samples.iloc[:,:-1].columns)\n",
    "wei_sf['weight'] =list(model1.coef_[0])\n",
    "bias =model1.intercept_[0]\n",
    "wei_sf =wei_sf.append(pd.DataFrame(index=['bias'],data=[bias],columns=['weight']))\n",
    "print bias\n",
    "wei_sf"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def calc_base(weight,sample):\n",
    "    sf = weight*pd.Series(sample)\n",
    "    base = sf.sum()\n",
    "    return base   \n",
    "def card_score(A,B,bias,base_score):\n",
    "    score = A-B*(bias+base_score)\n",
    "    return int(score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "result['base_score']= samples.apply(lambda x:calc_base(wei_sf['weight'],x),axis=1)\n",
    "result['card_score'] = result['base_score'].apply(lambda x:card_score(441.5,72.13,bias,x))\n",
    "result.to_csv('./out1/model_pre_all_samples_score_6.csv')\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#对应客户和时间\n",
    "feature_loan = pd.read_csv('./new_out/3m_loan_all.csv')\n",
    "tmp = feature_loan[['客户','应收单号','起算日期']].merge(result['score'].reset_index(),on='应收单号',how='right')\n",
    "tmp.to_csv('./out1/customer_score_times_6.csv',index=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "#对应客户旧评分卡分数\n",
    "df =pd.DataFrame()\n",
    "df['times'] = tmp.groupby(by='客户')['起算日期'].apply(list)\n",
    "df['scores'] = tmp.groupby(by='客户')['score'].apply(list)\n",
    "old_card = pd.read_excel('./data/feature_checked.xlsx')\n",
    "old_card.index = [x.encode('utf-8') for x in old_card.index]\n",
    "df =df.merge(old_card[u'评分'].reset_index(),left_index=True,right_on='index',how='left')\n",
    "df.to_csv('./out1/customer_score_times_6.csv',encoding='utf-8')\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 评分卡制作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "card =pd.read_csv('./out1/woes_cols_2.csv')\n",
    "card = card.merge(wei_sf.reset_index(),left_on='变量',right_on='index',how='right')\n",
    "# card['base'] = card['woe']*card['weight']*(-72.13)\n",
    "card.to_csv('./out1/card_6.csv')    \n",
    "card"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
